import datetime
import random

from django.test import TestCase
from syl_ideal import settings
# Create your tests here.
from alipay import AliPay



alipay_public_key_string = """-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAp9V+pNDb15nY//afR7UwecQ24vcPxG2YUMDRTqWGmMWlU/uFcvlXxRcU3Z969OXbci+xsLGWffCODCofZA9ZdDbYfDFCqv+n/72S4p4LPEISsT+Tll4ZAi/lUngiC+khiNAJP8oHATZl4WIRF6975Ew/+YCNF0AyPHn0QJ772sX5nD+xh+En4thzteV6aOaOtZw8MdZDGTgif1gvbf8NWPxVgq03WOhMZNK9V2GeGbP4glDkPxUI7A6OFu6kcK3pGFLQTJGALurcQOQUoDTsfwl8SqX0rplS6KVSyM17Pq4ETjSNkiZM1vU4k2Q27X2yYfJapybSFy2LgCK7KB4ykQIDAQAB
-----END PUBLIC KEY-----"""

app_private_key_string = """-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEAp9V+pNDb15nY//afR7UwecQ24vcPxG2YUMDRTqWGmMWlU/uFcvlXxRcU3Z969OXbci+xsLGWffCODCofZA9ZdDbYfDFCqv+n/72S4p4LPEISsT+Tll4ZAi/lUngiC+khiNAJP8oHATZl4WIRF6975Ew/+YCNF0AyPHn0QJ772sX5nD+xh+En4thzteV6aOaOtZw8MdZDGTgif1gvbf8NWPxVgq03WOhMZNK9V2GeGbP4glDkPxUI7A6OFu6kcK3pGFLQTJGALurcQOQUoDTsfwl8SqX0rplS6KVSyM17Pq4ETjSNkiZM1vU4k2Q27X2yYfJapybSFy2LgCK7KB4ykQIDAQABAoIBAF5XVQwCV0F3OpKPJ/Of9vLLh+rG4/cPf+oMqNsjkmg32Xo+zUHWxS6FjfYSGoSk3FjwVFq1SJncaYyO9PxC4I7B8ajF6+rBMMdB4do+6qPhpC48RZLlq/sdvzM/UiwNzQrhngFcZ9mXoWcyv9bnjqXC8scmKqkiLdru1oyokXvklnXciEXTcnQ+JqHrihe0OuIXSLzq439QAKfoRWWFjeA6GtbSDKjbC+3YeNlWAl2O14ZQLIVxnNUX1VVPd0kEHbKwjmEZsBI+HvPT6XRb5RSjKPF/j5GtpMvjJZOizASS35T8NAPRsJwpU72QvY/SEAaFND2j8c2GhtWXMqJcIAECgYEA95JY4+KlmVqnzyWpWQ3OSA9zunfbv6USpSKti+Zt7nXSHrrdwbOjDxxDShrWkgj32TmhWYuXYPuWe38Gv5rpXow2zkZ+SaAaaJ9hw4mQUlubtqhRU2UO9Jl9O7n+deP9R4yAGI90yx1l+F6RzE93z3bi0wOD7iaAwtK0tkXh5JECgYEArYw2VBopPmkNt5xN64hty5A39LZ4ymrGh5lFSkhw6l39y2VMxv46igfanUX10rBulwKHdAkf1ykwvI/TSt28UoSBWNP6ZX/5+n5Y5dcY8uOrKe09Lt/rNWONgoHZcKriaKiwfv1GbJfu26VVGDB+NIZqF8liAMafkGjJJ9ymbgECgYBcGAGfDAGkEVgZM55SdVNF9RGMzOde3A4ahdLfFuHP8d4u9j5lvugqR840M9Wbz2A7WzfNMqMmPZCV8gPVpM+pDymYBBsYUklgn+FjNroMGO3MvcI67n3ouWwfmqmwuxXN1tMTz9GXeSEkijFcOjeMYmIwMr+SCJqfXnhA0bAdMQKBgAe/kNVkbZ75tcES22++oZhLEhR86RuwdLitISjV3yS3mZMxLKJdy2NKQnpvGzCS6/0WAdIubht/HUvt5/MZv0S34caDgFRzyEdkyLdKSTTe8U9n5ilnmerCTRkcjwa2unENxwOEb1Z92g44bXotABV8QtRZC2UZeRpLc1+yPEQBAoGAHUI87P+AUYh2OVl0/b+zu/hY2zvStIwYhAktu7ybZcA8jGwm8ELDoGeAEg0wl+iddVIh5LuZ26pvbc7ftABPlGCjgF/pzFFUUDotqsRXFX/TbLc164W1gtGLKZYR3+jigAuotqY2FFU57Qp0YQq0Oim7fqAiqnhpMgtT1VZLbF4=
-----END RSA PRIVATE KEY-----"""

# 实例化支付应用
alipay = AliPay(
    # 你的应用的app_id
    appid="2016102600767438",
    # 异步通知的（据说支付宝的异步通知的准确率高达99.99）
    app_notify_url=None,
    # 生成的私钥
    app_private_key_string=app_private_key_string,
    # 支付宝的公钥
    alipay_public_key_string=alipay_public_key_string,
    # 加密的方式
    sign_type="RSA2"
)


def get_pay_url(out_trade_no, total_amount, subject):
    """
    out_trade_no='SYL202008241212120000005',
        total_amount=str(199),
        subject="实验楼订单号:%s" % 'SYL202008241212120000005',
    :param out_trade_no: 实验楼订单号:  SYL202008241212120000005
    :param total_amount: 总价: 299
    :param subject:  支付描述
    :return:
    """

    # 发起支付请求
    order_string = alipay.api_alipay_trade_page_pay(
        out_trade_no=out_trade_no,  # 订单号，多次请求不能一样
        total_amount=str(total_amount),  # 支付金额
        subject=subject,  # 交易主题
        return_url=settings.ALIPAY_RETURN_URL,  # 重定向
        notify_url=None  # 异步通知subject,
    )
    # 响应登录支付宝连接
    # 真实环境电脑网站支付网关：https://openapi.alipay.com/gateway.do? + order_string
    # 沙箱环境电脑网站支付网关：https://openapi.alipaydev.com/gateway.do? + order_string
    alipay_url = settings.ALIPAY_URL + "?" + order_string
    return alipay_url

    # print("https://openapi.alipaydev.com/gateway.do?"+order_string)


def get_order_id():
    """
    SYL202008241212121200005/24
    生成订单号: 格式: SYL + 年月日时分秒 + 5位随机数
    :return:
    """
    d = datetime.datetime.now()
    base = 'SYL'
    time_str = '%04d%02d%02d%02d%02d%02d' % (d.year, d.month, d.day, d.hour, d.minute, d.second)
    rand_num = str(random.randint(10000, 99999))
    return base + time_str + rand_num
